package com.example.denglu.util;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.HashMap;
import java.util.Map;

/**
 * 返回ssdp 响应参数对象
 */
public class ResponseData {
    /**
     * 响应码
     */
    private String returnCode;

    /**
     * 描述
     */
    private String returnDesc;

    /**
     * 时间戳
     */
    private String returnStamp = TranslateUtil.getTimeStamp();

    /**
     * 业务数据对象
     */
    private Object returnData = "";

    public ResponseData() {
    }

    public ResponseData(String returnCode, String returnDesc) {
        this.returnCode = returnCode;
        this.returnDesc = returnDesc;
    }

    public ResponseData(STATE state, Object returnData) {
        this.returnCode = state.code;
        this.returnDesc = state.desc;
        this.returnData = returnData;
    }


    public static ResponseData success() {
        return new ResponseData(STATE.SUC, "");
    }

    @JsonProperty("RETURN_CODE")
    public String getReturnCode() {
        return returnCode;
    }

    public void setReturnCode(String returnCode) {
        this.returnCode = returnCode;
    }

    @JsonProperty("RETURN_DESC")
    public String getReturnDesc() {
        return returnDesc;
    }

    public void setReturnDesc(String returnDesc) {
        this.returnDesc = returnDesc;
    }

    @JsonProperty("RETURN_STAMP")
    public String getReturnStamp() {
        return returnStamp;
    }

    public void setReturnStamp(String returnStamp) {
        this.returnStamp = returnStamp;
    }

    @JsonProperty("RETURN_DATA")
    public Object getReturnData() {
        return returnData;
    }

    public void setReturnData(Object returnData) {
        this.returnData = returnData;
    }

    public JSONObject toJSON() {
        JSONObject reustObj = new JSONObject();
        reustObj.put("RESPONSE", this);
        return reustObj;
    }

    public static JSONObject suc(Object o) {
        return new ResponseData(STATE.SUC, o).toJSON();
    }

    public static JSONObject fail(Object o) {
        return new ResponseData(STATE.FAIL, o).toJSON();
    }

    public enum STATE {
        SUC("S0A00000", "success"), // 成功
        FAIL("F0A00000", "failure"); // 失败
        public final String code;
        public final String desc;

        STATE(String code, String desc) {
            this.code = code;
            this.desc = desc;
        }

        private static Map<String, STATE> codeLookup = new HashMap<>();

        public static ResponseData.STATE forCode(String code) {
            return codeLookup.get(code);
        }

        static {
            for (ResponseData.STATE type : values()) {
                codeLookup.put(type.code, type);
            }
        }
    }
}
